from fastapi import FastAPI,staticfiles
import uvicorn
from pydantic import BaseModel, Field
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html

# 创建一个FastAPI实例
app = FastAPI(redoc_url=None, docs_url=None)

# 挂载静态路径映射
app.mount("/static", staticfiles.StaticFiles(directory="../statics"), name="static")

# 重新定义Swagger UI和Redoc路由
@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui():
    return get_swagger_ui_html(
        openapi_url = "/openapi.json",
        title = "Swagger UI for FastAPI",
        swagger_js_url="/static/swagger-ui-bundle.min.js",
        swagger_css_url="/static/swagger-ui.min.css"
    )

@app.get("/redoc", include_in_schema=False)
async def custom_redoc():
    return get_redoc_html(
        openapi_url = "/openapi.json",
        title = "Redoc for FastAPI",
        redoc_js_url="/static/redoc.standalone.js"
)

# json格式请求体，需要使用pydantic定义参数模型，然后在处理函数中指定参数的模型类型
# pydantic定义参数模型继承自BaseModel
class RegisterInfo(BaseModel):
    username: str
    password: str = Field(exclude=True)
    confirm_password: str = Field(exclude=True)
    email: str
    mobile: str


# 处理注册请求
@app.post("/api/users/register")
async def register(register_info: RegisterInfo):
    return {"message": "注册成功", "data": register_info}

if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)